package com.bii.gelapp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class InternalStorage implements Serializable {
	public static void writeObject(Context context, String key, Object object)
			throws IOException {

		FileOutputStream fos = context
				.openFileOutput(key, Context.MODE_PRIVATE);
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(object);

		oos.close();
		fos.close();
	}

	public static Object readObject(Context context, String key)
			throws IOException, ClassNotFoundException {
		FileInputStream fis = context.openFileInput(key);
		ObjectInputStream ois = new ObjectInputStream(fis);
		Object object = ois.readObject();
		fis.close();
		ois.close();
		return object;
	}

	public static void writeBitmap(Context context, String key, Bitmap bitmap)
			throws IOException {

		FileOutputStream out;
	    try {
	        out = context.openFileOutput(key, Context.MODE_PRIVATE);
	        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
	        out.close();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}

	public static Bitmap readBitmap(Context context, String key)
			throws IOException, ClassNotFoundException {
		try{
		    FileInputStream fis = context.openFileInput(key);
		        Bitmap b = BitmapFactory.decodeStream(fis);
		        fis.close();
		        return b;
		    }
		    catch(Exception e){
		    }
		    return null;
	}

	public static void writeStandardObject(Context context, String key,
			MarkerStandard object) throws IOException {
		FileOutputStream fos = context
				.openFileOutput(key, Context.MODE_PRIVATE);
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(object);

		oos.close();
		fos.close();
	}

	public static void deleteObject(Context context, String key)
			throws IOException, ClassNotFoundException {
		// FileOutputStream fos = context.openFileOutput(key,
		// Context.MODE_PRIVATE);

		// File file = new File(key);
		// OutputStream out = new FileOutputStream(file, false);
		// out.close();
		// boolean deleted = file.delete();
		// context.deleteFile(key);

		File dir = context.getFilesDir();
		File file = new File(dir, key);
		boolean deleted = file.delete();

	}

}
